package com.sinba.common.system.dao;

import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.system.dto.ExtractDTO;
import com.sinba.common.system.entity.Extract;
import com.sinba.common.util.StringUtils;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class ExtractDAO
    extends BaseDAOImplHibernate<Extract>
    implements IExtractDAO
{
    public PageDTO findPager( ExtractDTO qdto, int start, int limit, String sord, String sidx )
    {
        // TODO Auto-generated method stub
        final DetachedCriteria dc = DetachedCriteria.forClass( Extract.class );

        if ( qdto != null )
        {
            if ( ! qdto.getLoginName(  ).equals( "ALL" ) )
            {
                dc.add(Restrictions.or(
                		Restrictions.eq( "loginName",qdto.getLoginName(  ) ), 
                		Restrictions.eq( "parentAccount",qdto.getParentAccount(  ) ))
                );
            }
            if ( StringUtils.hasText( qdto.getStatus(  ) ) )
            {
                dc.add( Restrictions.eq( "status",
                                         qdto.getStatus(  ) ) );
            }
            
            //时间搜索
            if ( ( qdto.getStartTime(  ) != null ) && ( qdto.getEndTime(  ) == null ) )
            {
                dc.add( Restrictions.ge( "createTime",
                                         qdto.getStartTime(  ) ) );
            }

            if ( ( qdto.getStartTime(  ) == null ) && ( qdto.getEndTime(  ) != null ) )
            {
                Calendar endTimeCl = new GregorianCalendar(  );
                endTimeCl.setTime( qdto.getEndTime(  ) );
                endTimeCl.set( Calendar.DATE, endTimeCl.get( Calendar.DATE ) + 1 );
                dc.add( Restrictions.le( "createTime",
                                         endTimeCl.getTime(  ) ) );
            }

            if ( ( qdto.getStartTime(  ) != null ) && ( qdto.getEndTime(  ) != null ) )
            {
                Calendar endTimeCl = new GregorianCalendar(  );
                endTimeCl.setTime( qdto.getEndTime(  ) );
                endTimeCl.set( Calendar.DATE, endTimeCl.get( Calendar.DATE ) + 1 );

                dc.add( Restrictions.and( Restrictions.le( 
                                                           "createTime",
                                                           endTimeCl.getTime(  ) ),
                                          Restrictions.ge( 
                                                           "createTime",
                                                           qdto.getStartTime(  ) ) ) );
            }
        }

        if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) )
        {
            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }

        return super.findPageByCriteria( dc, start, limit );
    }

    //根据状�?统计
    public Integer extractCount( String status )
    {
        final DetachedCriteria dc = DetachedCriteria.forClass( Extract.class );
        dc.add( Restrictions.eq( "status", status ) );

        return super.statCriteria( dc );
    }
    
    /**
     * 判断今天是否已提现
     * @param loginName
     * @return
     */
    public boolean todayIsTiXian(String loginName){
    	final DetachedCriteria dc = DetachedCriteria.forClass( Extract.class );
    	dc.add(Restrictions.eq("loginName", loginName));
    	dc.add( Restrictions.in( "status", new String[]{"待受理","已处理"} ) );
    	Calendar timeStartCl = new GregorianCalendar(  );
        timeStartCl.setTime( new Date(  ) );
        timeStartCl.set( Calendar.HOUR_OF_DAY, 0 );
        timeStartCl.set( Calendar.MINUTE, 0 );
        timeStartCl.set( Calendar.SECOND, 0 );

        Calendar timeEndCl = new GregorianCalendar(  );
        timeEndCl.setTime( new Date(  ) );
        timeEndCl.set( Calendar.HOUR_OF_DAY, 23 );
        timeEndCl.set( Calendar.MINUTE, 59 );
        timeEndCl.set( Calendar.SECOND, 59 );

        dc.add( Restrictions.between( "createTime",
                                      timeStartCl.getTime(  ),
                                      timeEndCl.getTime(  ) ) );
    	Integer count = super.statCriteria( dc );
    	if(count!=null && count>0){
    		return true;
    	}else{
    		return false;
    	}
    }
    /**
     * 统计全部提现金额
     * @param loginName
     * @return
     */
    public Double statAllTiXing(String loginName){
    	String hql = "select sum(tixiangSum) from Extract where status in ('待受理','已处理') and formAccount='Static' and loginName='"+loginName+"'";
    	Double result = 0D;
		try {
			List<Object> objs = super.getHibernateTemplate().find(hql);
			for(Object obj : objs){
				result = (Double)obj;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("没有找到已提现的金额");
		}
		if(result==null){
			result = 0D;
		}
		return result;
    }
    
}
